﻿
using IdentityModel.Client;
using IdentityModel.OidcClient;

namespace ConsoleApp
{
    internal class myProg
    {
        string _authority = "http://host.docker.internal:7010";
        OidcClient _oidcClient;
        public async Task<LoginResult> SignIn()
        {
            var browser = new SystemBrowser(5555);
            string redirectUri = string.Format($"http://127.0.0.1:{browser.Port}");

            var options = new OidcClientOptions
            {
                Authority = _authority,
                ClientId = "poemgamewinform",// "interactive.public",
                RedirectUri = redirectUri,
                Scope = "openid profile poemapi",
                Policy = new Policy
                {
                    Discovery = new DiscoveryPolicy { RequireHttps = false }
                },//disable https
                Browser = browser,
            };

            _oidcClient = new OidcClient(options);
            var result = await _oidcClient.LoginAsync(new LoginRequest());
            return result;
        }

        public static void ShowResult(LoginResult result)
        {
            if (result.IsError)
            {
                Console.WriteLine("\n\nError:\n{0}", result.Error);
                return;
            }

            Console.WriteLine("\n\nClaims:");
            foreach (var claim in result.User.Claims)
            {
                Console.WriteLine("{0}: {1}", claim.Type, claim.Value);
            }

            Console.WriteLine($"\nidentity token: {result.IdentityToken}");
            Console.WriteLine($"access token:   {result.AccessToken}");
            Console.WriteLine($"refresh token:  {result?.RefreshToken ?? "none"}");
        }

    }
}
